package com.blb.controller.front;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.blb.entity.*;
import com.blb.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/front/index")
public class IndexController {

    @Autowired
    private IFrontUserService frontUserService;

    @Autowired
    private IKeywordService keywordService;
    @Autowired
    private IProductTypeService productTypeService;

    @Autowired
    private IGoodstypeService goodstypeService;

    @Autowired
    private ICommodityService commodityService;

    @Autowired
    private IBrandService brandService;

    @Autowired
    private IBrandTypeService brandTypeService;

    @Autowired
    private IComimgService comimgService;

    @Autowired
    private ICartComService cartComService;

    /**
     * 点击首页分类跳转商品分类
     * @param comtypeid
     * @param model
     * @return
     */
    @RequestMapping("clickComtype")
    public String comtype(String comtypeid, Model model){

        //关键词根据次数排序前十条数据
        List<Keyword> keywordTop = keywordService.getKeywordTop();
        model.addAttribute("keywordTop",keywordTop);

        //获取分类的前九条数据
        List<Comtype> comtype = productTypeService.getComtype();
        model.addAttribute("comtypes",comtype);

        for (Comtype com : comtype){
            if(com.getId().equals(comtypeid)){
                model.addAttribute("comtypeName",com.getName());
            }
        }

        //根据商品类型获取品牌
        List<BrandType> brandTypes = brandTypeService.list(new QueryWrapper<BrandType>().eq("type_id", comtypeid));
        List<String> brandids = new ArrayList<>();
        for (BrandType brandType : brandTypes){
            brandids.add(brandType.getBrandId());
        }
        List<Brand> brandLists = brandService.list(new QueryWrapper<Brand>().in("id", brandids));
        model.addAttribute("brandLists",brandLists);

        //根据商品类型获取商品信息
        List<Goodstype> goodstypes = goodstypeService.list(new QueryWrapper<Goodstype>().eq("type_id", comtypeid));
        List<String> commodityids = new ArrayList<>();
        for (Goodstype goodstype : goodstypes){
            commodityids.add(goodstype.getComId());
        }
        List<Commodity> commodityLists = commodityService.list(new QueryWrapper<Commodity>().in("id", commodityids));
        model.addAttribute("commodityLists",commodityLists);

        return "front/classify";
    }

    /**
     * 点击商品跳转商品详情
     * @param commodityId
     * @return
     */
    @RequestMapping("commodityDetails")
    public String goCommodityDetails(String commodityId,Model model){

        //关键词根据次数排序前十条数据
        List<Keyword> keywordTop = keywordService.getKeywordTop();
        model.addAttribute("keywordTop",keywordTop);

        //获取分类的前九条数据
        List<Comtype> comtype = productTypeService.getComtype();
        model.addAttribute("comtypes",comtype);

        //商品信息
        Commodity commodity = commodityService.getOne(new QueryWrapper<Commodity>().eq("id", commodityId));
        model.addAttribute("commodity",commodity);

        //详细商品信息
        List<Comimg> comimgs = comimgService.list(new QueryWrapper<Comimg>().eq("com_id", commodityId).orderByAsc("seq"));
        model.addAttribute("comimgs",comimgs);

        return  "front/goodsDetail";
    }


}
